package cn.edu.cug.cs.gtl.docsrv.dict.service.impl;

import cn.edu.cug.cs.gtl.docsrv.dict.dao.DictCatalogDao;
import cn.edu.cug.cs.gtl.docsrv.dict.service.DictCatalogService;
import cn.edu.cug.cs.gtl.mybatis.mapper.dict.DictCatalog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Component
public class DictCatalogServiceImpl  implements DictCatalogService {
    @Autowired
    DictCatalogDao dictCatalogDao;

    @Override
    public Object getMasterCatalogs() throws Exception {
        List<DictCatalog> catalogs = dictCatalogDao.getMasterCatalogs();
        return catalogs;
//        List<Object> result = new ArrayList<>();
//
//        for (DictCatalog tab : catalogs) {
//            String route = tab.getCatalogRoute();
//            List objects = result;
//            if (!StringUtils.isEmpty(route)) {
//                String[] str = route.split("/");
//
//                for (String c : str) {
//                    Map<String, Object> mm = null;
//                    for (Object obj : objects) {
//                        if (((Map) obj).get("nameCn").equals(c)) {
//                            mm = (Map) obj;
//                            break;
//                        }
//                    }
//
//                    if (mm == null) {
//                        mm = new LinkedHashMap<>();
//                        mm.put("nameCn", c);
//                        objects.add(mm);
//                    }
//                    mm.computeIfAbsent("children", k -> new ArrayList<>());
//                    objects = (List) mm.get("children");
//                }
//            }
//            Map<String, Object> m = new LinkedHashMap<>();
//            m.put("nameCn", tab.getCatalogCnName());
//            m.put("nameEn", tab.getCatalogEnName());
//            m.put("ref", tab.getCatalogUrl());
//            m.put("children", null);
//            objects.add(m);
//        }
//        return result;
    }


    @Override
    public Object getSlaveCatalogs(String slaveDataSourceName) throws Exception {
        List<DictCatalog> catalogs = dictCatalogDao.getSlaveCatalogs(slaveDataSourceName);
        return catalogs;
//        List<Object> result = new ArrayList<>();
//
//        for (DictCatalog tab : catalogs) {
//            String route = tab.getCatalogRoute();
//            List objects = result;
//            if (!StringUtils.isEmpty(route)) {
//                String[] str = route.split("/");
//
//                for (String c : str) {
//                    Map<String, Object> mm = null;
//                    for (Object obj : objects) {
//                        if (((Map) obj).get("nameCn").equals(c)) {
//                            mm = (Map) obj;
//                            break;
//                        }
//                    }
//
//                    if (mm == null) {
//                        mm = new LinkedHashMap<>();
//                        mm.put("nameCn", c);
//                        objects.add(mm);
//                    }
//                    mm.computeIfAbsent("children", k -> new ArrayList<>());
//                    objects = (List) mm.get("children");
//                }
//            }
//            Map<String, Object> m = new LinkedHashMap<>();
//            m.put("nameCn", tab.getCatalogCnName());
//            m.put("nameEn", tab.getCatalogEnName());
//            m.put("ref", tab.getCatalogUrl());
//            m.put("children", null);
//            objects.add(m);
//        }
//        return result;
    }


    @Override
    public Object getMasterCatalogsV1() throws Exception {
        List<DictCatalog> catalogs = dictCatalogDao.getMasterCatalogs();

        List<Object> result = new ArrayList<>();

        for (DictCatalog tab : catalogs) {
            String route = tab.getCatalogRoute();
            List objects = result;
            if (!StringUtils.isEmpty(route)) {
                String[] str = route.split("/");

                for (String c : str) {
                    Map<String, Object> mm = null;
                    for (Object obj : objects) {
                        if (((Map) obj).get("nameCn").equals(c)) {
                            mm = (Map) obj;
                            break;
                        }
                    }

                    if (mm == null) {
                        mm = new LinkedHashMap<>();
                        mm.put("nameCn", c);
                        objects.add(mm);
                    }
                    mm.computeIfAbsent("children", k -> new ArrayList<>());
                    objects = (List) mm.get("children");
                }
            }
            Map<String, Object> m = new LinkedHashMap<>();
            m.put("nameCn", tab.getCatalogCnName());
            m.put("nameEn", tab.getCatalogEnName());
            m.put("ref", tab.getCatalogUrl());
            m.put("children", null);
            objects.add(m);
        }
        return result;
    }


    @Override
    public Object getSlaveCatalogsV1(String slaveDataSourceName) throws Exception {
        List<DictCatalog> catalogs = dictCatalogDao.getSlaveCatalogs(slaveDataSourceName);
        List<Object> result = new ArrayList<>();

        for (DictCatalog tab : catalogs) {
            String route = tab.getCatalogRoute();
            List objects = result;
            if (!StringUtils.isEmpty(route)) {
                String[] str = route.split("/");

                for (String c : str) {
                    Map<String, Object> mm = null;
                    for (Object obj : objects) {
                        if (((Map) obj).get("nameCn").equals(c)) {
                            mm = (Map) obj;
                            break;
                        }
                    }

                    if (mm == null) {
                        mm = new LinkedHashMap<>();
                        mm.put("nameCn", c);
                        objects.add(mm);
                    }
                    mm.computeIfAbsent("children", k -> new ArrayList<>());
                    objects = (List) mm.get("children");
                }
            }
            Map<String, Object> m = new LinkedHashMap<>();
            m.put("nameCn", tab.getCatalogCnName());
            m.put("nameEn", tab.getCatalogEnName());
            m.put("ref", tab.getCatalogUrl());
            m.put("children", null);
            objects.add(m);
        }
        return result;
    }
}
